// installation of packages

sysdir set PLUS "~\Settings\Stata\ado"
ssc install estout, all replace
ssc install vreverse, all replace
ssc install cibar, all replace
ssc install coefplot, all replace
ssc install combomarginsplot, all replace
ssc install mplotoffset, all replace
ssc install alphawgt, all replace
ssc install palettes, all replace
ssc install colrspace, all replace
ssc install rowranks, all replace

**************************
*** 0_DATA PREPARATION ***
**************************

** retrieve ESS data via Datafile Builder in ESS Data Portal; select all rounds and all countries, then download:
** https://ess.sikt.no/en/data-builder/?rounds=0+1+2+3+4+5+6+7+8+9+10+11&seriesVersion=942&tab=download
** unpack to working directory

cd "~"
use ESS1e06_7-ESS2e03_6-ESS3e03_7-ESS4e04_6-ESS5e03_5-ESS6e02_6-ESS7e02_3-ESS8e02_3-ESS9e03_2-ESS10-ESS10SC-ESS11.dta, clear

replace anweight = pspwght*pweight if anweight == .

encode cntry, gen(land)
label list land
label define land ///
1 "Albania" ///
2 "Austria" ///
3 "Belgium" ///
4 "Bulgaria" ///
5 "Switzerland" ///
6 "Cyprus" ///
7 "Czechia" ///
8 "Germany" ///
9 "Denmark" ///
10 "Estonia" ///
11 "Spain" ///
12 "Finland" ///
13 "France" ///
14 "United Kingdom" ///
15 "Greece" ///
16 "Croatia" ///
17 "Hungary" ///
18 "Ireland" ///
19 "Israel" ///
20 "Iceland" ///
21 "Italy" ///
22 "Lithuania" ///
23 "Luxembourg" ///
24 "Latvia" ///
25 "Montenegro" ///
26 "North Macedonia" ///
27 "Netherlands" ///
28 "Norway" ///
29 "Poland" ///
30 "Portugal" ///
31 "Romania" ///
32 "Serbia" ///
33 "Russian Federation" ///
34 "Sweden" ///
35 "Slovenia" ///
36 "Slovakia" ///
37 "Turkey" ///
38 "Ukraine" ///
39 "Kosovo", modify

gen year = 2002 if essround == 1
	replace year = 2004 if essround == 2
	replace year = 2006 if essround == 3
	replace year = 2008 if essround == 4
	replace year = 2010 if essround == 5
	replace year = 2012 if essround == 6
	replace year = 2014 if essround == 7
	replace year = 2016 if essround == 8
	replace year = 2018 if essround == 9
	replace year = 2020 if essround == 10
	replace year = 2022 if essround == 11
format year %ty
lab var year "Year"

** Table A2
tab land year
gen rounds = .
replace rounds = 11 if inlist(land,3,5,8,11,12,13,14,17,18,27,28,29,30,34,35)
replace rounds = 9 if inlist(land,7,10)
replace rounds = 8 if inlist(land,2,9,36)
replace rounds = 7 if inlist(land,6,19,22)
replace rounds = 6 if inlist(land,4,15,20,21)
replace rounds = 5 if inlist(land,16,33,38)
replace rounds = 3 if inlist(land,24,32)
replace rounds = 2 if inlist(land,23,25,37)
replace rounds = 1 if inlist(land,1,26,31,39)
lab var rounds "Number of ESS rounds"

sum yrbrn, d
gen gener5 = .
replace gener5 = 1 if inrange(yrbrn,1900,1945)
replace gener5 = 2 if inrange(yrbrn,1946,1964)
replace gener5 = 3 if inrange(yrbrn,1965,1979)
replace gener5 = 4 if inrange(yrbrn,1980,1996)
replace gener5 = 5 if inrange(yrbrn,1997,2009)
lab define gener5 1 "Interwar" 2 "Baby Boomers" 3 "Generation X" 4 "Millennials" 5 "Generation Z"
lab val gener5 gener5
lab var gener5 "Generational cohorts"

	* combining cohorts due to number of observations for Part 2-analyses
	gen gener4 = .
	replace gener4 = gener5
	replace gener4 = 4 if gener5 == 4 | gener5 == 5
	label define gener4 1 "Interwar" 2 "Baby Boomers" 3 "Generation X" 4 "Millennials/Generation Z"
	label values gener4 gener4
	
	gen gener3 = .
	replace gener3 = 1 if gener5 == 1 | gener5 == 2
	replace gener3 = 2 if gener5 == 3
	replace gener3 = 3 if gener5 == 4 | gener5 == 5
	label define gener3 1 "Interwar/Baby Boomers" 2 "Generation X" 3 "Millennials/Generation Z"
	label values gener3 gener3

sum lrscale, d
* grouping while simultaneously allowing for enough observations in each group for Part 2-analyses
gen leftright = .
replace leftright = 1 if lrscale >= 0 & lrscale <= 2
replace leftright = 2 if lrscale >= 3 & lrscale <= 4
replace leftright = 3 if lrscale == 5
replace leftright = 4 if lrscale >= 6 & lrscale <= 7
replace leftright = 5 if lrscale >= 8 & lrscale <= 10
label define leftright 1 "Left" 2 "Center-left" 3 "Center" 4 "Center-right" 5 "Right"
label values leftright leftright

gen extremity = abs(lrscale - 5)
lab define extremity 0 "Moderate" 1 " " 2 " " 3 " " 4 " " 5 "Extreme"
lab val extremity extremity
lab var extremity "Extremity"

tab clsprty
recode clsprty (2=0 "Non-partisan") (1=1 "Partisan"), gen(partisan)
lab var partisan "Partisanship (Closeness to particular party)"

tab polintr, nolab
vreverse polintr, gen(hintr)
gen intr = hintr-1
lab define intr 0 "Not at all interested" 1 "Hardly interested" 2 "Quite interested" 3 "Very interested"
lab val intr intr
lab var intr "Political interest"

recode intr (0 1 = 0 "Not interested") (2 3 = 1 "Interested"), gen(intr_d)
lab var intr_d "Political interest (Dummy)"

recode gndr (1=0 "Male") (2=1 "Female"), gen(sex)
lab var sex "Gender"

** Cultural attitudes
* a) Environmentalism
recode impenv (6=0 "Not like me at all") (5=1 "Not like me") (4=2 "A little like me") (3=3 "Somewhat like me") (2=4 "Like me") (1=5 "Very much like me"), gen(env)
recode impenva (6=0 "Not like me at all") (5=1 "Not like me") (4=2 "A little like me") (3=3 "Somewhat like me") (2=4 "Like me") (1=5 "Very much like me"), gen(env2022)
replace env = env2022 if env == .
lab var env "Environmentalism"

gen env_norm = env/5
lab var env_norm "Environmentalism (nrm.)"

* b) Immigration
labelbook imueclt // no need for changes

gen imueclt_norm = imueclt/10
lab var imueclt_norm "Immigration (nrm.)"

* c) Gender rights
*sysdir set PLUS "C:\Users\duy021\OneDrive - University of Bergen\Settings\Stata\ado"
*ssc install vreverse
vreverse freehms, gen(hmsfree)
gen grhom = hmsfree-1
lab define grhom 0 "Disagree strongly" 1 "Disagree" 2 "Neither agree nor disagree" 3 "Agree" 4 "Agree strongly"
lab val grhom grhom
lab var grhom "Homosexuality"

gen grhom_norm = grhom/4
lab var grhom_norm "Homosexuality (nrm.)"

* d) International cooperation
labelbook trstun // no need for changes

gen trstun_norm = trstun/10
lab var trstun_norm "United Nations (nrm.)"

* e) Climate change
recode ccnthum (1=0 "Entirely by natural processes") (2=1 "Mainly by natural processes") (3=2 "About equally by natural processes and human activity") (4=3 "Mainly by human activity") (5=4 "Entirely by human acitivity") (55=.e "I don't think climate change is happening"), gen(cchum)
lab var cchum "Anthropogenic cause"

gen ccwor = wrclmch-1
lab define ccwor 0 "Not at all worried" 1 "Not very worried" 2 "Somewhat worried" 3 "Very worried" 4 "Extremely worried"
lab val ccwor ccwor
lab var ccwor "Worry"

labelbook ccrdprs // no need for changes

gen ccwor_norm = ccwor/4
gen ccpers_norm = ccrdprs/10
gen cchum_norm = cchum/4
lab var ccwor_norm "Worry (nrm.)"
lab var ccpers_norm "Personal responsibility (nrm.)"
lab var cchum_norm "Anthropogenic cause (nrm.)"

gen cc_index = (1/3)*ccwor_norm + (1/3)*ccpers_norm + (1/3)*cchum_norm
lab var cc_index "Climate change (index)"

gen env_im  = .
gen env_hom = .
gen env_un  = .
gen im_hom  = .
gen im_un   = .
gen hom_un  = .
gen cc_env 	= .
gen cc_im	= .
gen cc_hom	= .
gen cc_un	= .

lab var env_im "Env.–Imm."
lab var env_hom "Env.–Hom."
lab var env_un "Env.–UN"
lab var im_hom "Imm.–Hom."
lab var im_un "Imm.–UN"
lab var hom_un "Hom.–UN"
lab var cc_env "Clim. ch.–Env."
lab var cc_im "Clim. ch.–Imm."
lab var cc_hom "Clim. ch.–Hom."
lab var cc_un "Clim. ch.–UN"

** Economic attitudes
* a) Egalitarianism
recode ipeqopt (6=0 "Not like me at all") (5=1 "Not like me") (4=2 "A little like me") (3=3 "Somewhat like me") (2=4 "Like me") (1=5 "Very much like me"), gen(egal)
recode ipeqopta (6=0 "Not like me at all") (5=1 "Not like me") (4=2 "A little like me") (3=3 "Somewhat like me") (2=4 "Like me") (1=5 "Very much like me"), gen(egal2022)
replace egal = egal2022 if egal == .
lab var egal "Egalitarianism"

gen egal_norm = egal/5
lab var egal_norm "Egalitarianism (nrm.)"

* b) Redistribution
recode gincdif (5=0 "Disagree strongly") (4=1 "Disagree") (3=2 "Neither agree nor disagree") (2=3 "Agree") (1=4 "Agree strongly"), gen(redis)
lab var redis "Redistribution"

gen redis_norm = redis/4
lab var redis_norm "Redistribution (nrm.)"

gen egal_redis  = .
lab var egal_redis "Egal.–Redis."

save 0_prepared.dta, replace

collapse env_norm imueclt_norm grhom_norm trstun_norm cc_index ccwor_norm ccpers_norm cchum_norm egal_norm redis_norm, by(land year)
xtset land year, delta(2)
xtsum 

				*****************************
				*** PAIRWISE CORRELATIONS ***
				***** COUNTRY-SPECIFIC ******
				*****************************
use 0_prepared.dta, clear

levelsof land, local(countries)
levelsof year, local(years)

* Pairwise correlations, belief system A
foreach l of local countries {
    foreach i of local years {
		if inlist(`i',2002,2004,2006,2008,2010,2012,2014,2018) {
        quietly {
			capture{

		corr env_norm imueclt_norm grhom_norm trstun_norm if land == `l' & year == `i' [aw=anweight]
        matrix m = r(C)
            
            replace env_im  = m[2,1] if land == `l' & year == `i'
            replace env_hom = m[3,1] if land == `l' & year == `i'
            replace env_un  = m[4,1] if land == `l' & year == `i'
            replace im_hom  = m[3,2] if land == `l' & year == `i'
            replace im_un   = m[4,2] if land == `l' & year == `i'
            replace hom_un  = m[4,3] if land == `l' & year == `i'
			}
			}
        }
    }
}

* Pairwise correlations, belief system B
foreach l of local countries {
    foreach i of local years {
        if inlist(`i',2016,2020,2022) {
            quietly {
				capture{
            
			corr env_norm imueclt_norm grhom_norm trstun_norm cc_index if land == `l' & year == `i' [aw=anweight]
            matrix m = r(C)
                
				replace env_im  = m[2,1] if land == `l' & year == `i'
				replace env_hom = m[3,1] if land == `l' & year == `i'
				replace env_un  = m[4,1] if land == `l' & year == `i'
				replace im_hom  = m[3,2] if land == `l' & year == `i'
				replace im_un   = m[4,2] if land == `l' & year == `i'
				replace hom_un  = m[4,3] if land == `l' & year == `i'
				
				replace cc_env = m[5,1] if land == `l' & year == `i'
                replace cc_im  = m[5,2] if land == `l' & year == `i'
                replace cc_hom = m[5,3] if land == `l' & year == `i'
                replace cc_un  = m[5,4] if land == `l' & year == `i'
				}
            }
        }
    }
}

foreach l of local countries {
    foreach i of local years {
        quietly {
			capture{

		corr egal_norm redis_norm if land == `l' & year == `i' [aw=anweight]
        matrix m = r(C)
            
            replace egal_redis = m[2,1] if land == `l' & year == `i'
			}
        }
    }
}

bysort year: tab land if egal_redis == .

save 1_p1_cntry.dta, replace

				*****************************
				*** PAIRWISE CORRELATIONS ***
				******  GROUP-SPECIFIC ******
				*****************************
use 0_prepared.dta, clear

keep if !missing(lrscale,partisan,intr,gener5,sex)

keep land year anweight leftright partisan intr gener5 gener4 gener3 sex ///
	env_norm imueclt_norm grhom_norm trstun_norm cc_index ///
	env_im env_un env_hom im_un im_hom hom_un cc_env cc_im cc_hom cc_un

levelsof land, local(countries)
levelsof year, local(years)

levelsof leftright, local(leftright_lvls)
levelsof partisan, local(partisan_lvls)
levelsof intr, local(intr_lvls)
levelsof gener5, local(gener5_lvls)
levelsof gener4, local(gener4_lvls)
levelsof gener3, local(gener3_lvls)
levelsof sex, local(sex_lvls)

* Pairwise correlations, belief system B
foreach l of local countries {
    foreach i of local years {
	if `i' >= 2016 { 
		foreach a of local leftright_lvls {
		foreach b of local partisan_lvls {
		foreach c of local intr_lvls {
		foreach d of local gener5_lvls {
		foreach e of local sex_lvls {
		quietly {
			capture{
				
			corr cc_index imueclt_norm grhom_norm trstun_norm env_norm ///
			if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener5 == `d' & sex == `e' [aw=anweight]
            matrix m = r(C) 
			
            replace cc_im  = m[2,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener5 == `d' & sex == `e'
            replace cc_hom = m[3,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener5 == `d' & sex == `e'
            replace cc_un  = m[4,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener5 == `d' & sex == `e'
            replace cc_env = m[5,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener5 == `d' & sex == `e'
			}
			}
		}
		}
		}
		}
		}
		}
	}
}

* Pairwise correlations, belief system A
foreach l of local countries {
    foreach i of local years {
		foreach a of local leftright_lvls {
		foreach b of local partisan_lvls {
		foreach c of local intr_lvls {
		foreach d of local gener5_lvls {
		foreach e of local sex_lvls {
		quietly {
			capture{

		corr env_norm imueclt_norm grhom_norm trstun_norm ///
		if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener5 == `d' & sex == `e' [aw=anweight]
        matrix m = r(C)
            
            replace env_im  = m[2,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener5 == `d' & sex == `e'
            replace env_hom = m[3,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener5 == `d' & sex == `e'
            replace env_un  = m[4,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener5 == `d' & sex == `e'
            replace im_hom  = m[3,2] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener5 == `d' & sex == `e'
            replace im_un   = m[4,2] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener5 == `d' & sex == `e'
            replace hom_un  = m[4,3] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener5 == `d' & sex == `e'
			}
		}
		}
		}
		}
		}
		}
	}
}

* FOUR GENERATIONAL COHORTS
gen env_im_gener4  = .
gen env_hom_gener4 = .
gen env_un_gener4  = .
gen im_hom_gener4  = .
gen im_un_gener4   = .
gen hom_un_gener4  = .
gen cc_env_gener4  = .
gen cc_im_gener4   = .
gen cc_hom_gener4  = .
gen cc_un_gener4   = .

* Pairwise correlations, belief system B
foreach l of local countries {
    foreach i of local years {
	if `i' >= 2016 { 
		foreach a of local leftright_lvls {
		foreach b of local partisan_lvls {
		foreach c of local intr_lvls {
		foreach d of local gener4_lvls {
		foreach e of local sex_lvls {
		quietly {
			capture{
				
			corr cc_index imueclt_norm grhom_norm trstun_norm env_norm ///
			if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener4 == `d' & sex == `e' [aw=anweight]
            matrix m = r(C) 
			
            replace cc_im_gener4  = m[2,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener4 == `d' & sex == `e'
            replace cc_hom_gener4 = m[3,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener4 == `d' & sex == `e'
            replace cc_un_gener4  = m[4,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener4 == `d' & sex == `e'
            replace cc_env_gener4 = m[5,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener4 == `d' & sex == `e'
			}
			}
		}
		}
		}
		}
		}
		}
	}
}

* Pairwise correlations, belief system A
foreach l of local countries {
    foreach i of local years {
		foreach a of local leftright_lvls {
		foreach b of local partisan_lvls {
		foreach c of local intr_lvls {
		foreach d of local gener4_lvls {
		foreach e of local sex_lvls {
		quietly {
			capture{

		corr env_norm imueclt_norm grhom_norm trstun_norm ///
		if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener4 == `d' & sex == `e' [aw=anweight]
        matrix m = r(C)
            
            replace env_im_gener4  = m[2,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener4 == `d' & sex == `e'
            replace env_hom_gener4 = m[3,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener4 == `d' & sex == `e'
            replace env_un_gener4  = m[4,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener4 == `d' & sex == `e'
            replace im_hom_gener4  = m[3,2] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener4 == `d' & sex == `e'
            replace im_un_gener4   = m[4,2] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener4 == `d' & sex == `e'
            replace hom_un_gener4  = m[4,3] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener4 == `d' & sex == `e'
			}
		}
		}
		}
		}
		}
		}
	}
}

* THREE GENERATIONAL COHORTS
gen env_im_gener3  = .
gen env_hom_gener3 = .
gen env_un_gener3  = .
gen im_hom_gener3  = .
gen im_un_gener3   = .
gen hom_un_gener3  = .
gen cc_env_gener3  = .
gen cc_im_gener3   = .
gen cc_hom_gener3  = .
gen cc_un_gener3   = .

* Pairwise correlations, belief system B
foreach l of local countries {
    foreach i of local years {
	if `i' >= 2016 { 
		foreach a of local leftright_lvls {
		foreach b of local partisan_lvls {
		foreach c of local intr_lvls {
		foreach d of local gener3_lvls {
		foreach e of local sex_lvls {
		quietly {
			capture{
				
			corr cc_index imueclt_norm grhom_norm trstun_norm env_norm ///
			if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener3 == `d' & sex == `e' [aw=anweight]
            matrix m = r(C) 
			
            replace cc_im_gener3  = m[2,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener3 == `d' & sex == `e'
            replace cc_hom_gener3 = m[3,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener3 == `d' & sex == `e'
            replace cc_un_gener3  = m[4,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener3 == `d' & sex == `e'
            replace cc_env_gener3 = m[5,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener3 == `d' & sex == `e'
			}
			}
		}
		}
		}
		}
		}
		}
	}
}

* Pairwise correlations, belief system A
foreach l of local countries {
    foreach i of local years {
		foreach a of local leftright_lvls {
		foreach b of local partisan_lvls {
		foreach c of local intr_lvls {
		foreach d of local gener3_lvls {
		foreach e of local sex_lvls {
		quietly {
			capture{

		corr env_norm imueclt_norm grhom_norm trstun_norm ///
		if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener3 == `d' & sex == `e' [aw=anweight]
        matrix m = r(C)
            
            replace env_im_gener3  = m[2,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener3 == `d' & sex == `e'
            replace env_hom_gener3 = m[3,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener3 == `d' & sex == `e'
            replace env_un_gener3  = m[4,1] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener3 == `d' & sex == `e'
            replace im_hom_gener3  = m[3,2] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener3 == `d' & sex == `e'
            replace im_un_gener3   = m[4,2] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener3 == `d' & sex == `e'
            replace hom_un_gener3  = m[4,3] if land == `l' & year == `i' & leftright == `a' & partisan == `b' & intr == `c' & gener3 == `d' & sex == `e'
			}
		}
		}
		}
		}
		}
		}
	}
}

save 1_p2_subgroups.dta, replace